package com.zhu.salesmanagement.adapters;

import java.util.ArrayList;

import com.dms2.salesmanagement.R;
import com.zhu.salesmanagement.models.ProductInfo;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class ProductInfoListAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private ArrayList<ProductInfo> lists;
	
	public ProductInfoListAdapter(Context context, ArrayList<ProductInfo> lists) {
		super();
		this.context = context;
		this.lists = lists;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int index, View view, ViewGroup arg2) {
		// TODO Auto-generated method stub
	    ProductInfo info = lists.get(index);
		if(view == null){
		    if(index == 0){
		        view = inflater.inflate(R.layout.productinfolist_item_first, null);
		    }else{
		        view = inflater.inflate(R.layout.productinfolist_item, null);
		    }
		}
		view.setBackgroundColor(Color.WHITE);
		updateViews(view, index, info, false);
		return view;
	}

	private void updateViews(View view, int index, ProductInfo info, boolean isSetListener){
		TextView textView1 = (TextView) view.findViewById(R.id.text1);
		EditText textView2 = (EditText) view.findViewById(R.id.text2);
		textView2.setTag(info);
		textView2.setBackgroundDrawable(null);
		
		EditText textView3 = (EditText) view.findViewById(R.id.text3);
		textView3.setTag(info);
		textView3.setBackgroundDrawable(null);

		EditText textView4 = (EditText) view.findViewById(R.id.text4);
		textView4.setTag(info);
		textView4.setBackgroundDrawable(null);

		EditText textView5 = (EditText) view.findViewById(R.id.text5);
		textView5.setTag(info);
		textView5.setBackgroundDrawable(null);

		textView1.setTextColor(Color.BLACK);
		textView2.setTextColor(Color.BLACK);
		textView3.setTextColor(Color.BLACK);
		textView4.setTextColor(Color.BLACK);
		textView5.setTextColor(Color.BLACK);

		textView1.setText(info.mName);
		textView2.setText(info.mFloor);
		textView3.setText(info.mSide);
		textView4.setText(info.mStock);
		textView5.setText(info.mPrice);

		if(index == 0){
			view.setBackgroundColor(0xff666666);
			textView1.setEnabled(false);
			textView2.setEnabled(false);
			textView3.setEnabled(false);
			textView4.setEnabled(false);
			textView5.setEnabled(false);

			textView1.setTextColor(Color.WHITE);
			textView2.setTextColor(Color.WHITE);
			textView3.setTextColor(Color.WHITE);
			textView4.setTextColor(Color.WHITE);
			textView5.setTextColor(Color.WHITE);
		}else{
			if(index%2 != 0){
				view.setBackgroundColor(Color.argb(250 ,  255 ,  255 ,  255 )); 
			}else{
				view.setBackgroundColor(Color.argb(250 ,  224 ,  243 ,  250 ));    
			}
		}
	}

	class MyTextWatcher implements TextWatcher { 
		private ProductInfo mProductInfo =null;

	    public MyTextWatcher(ProductInfo info) {
	    	mProductInfo = info;
	    }

	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    }

	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	    }

	    @Override
	    public void afterTextChanged(Editable s) {
	     if (s != null && !"".equals(s.toString())) {
	     }
	    }
	}
}
